package cn.tt.tuantuanerp.passport.webapi.controller;


import cn.tt.tuantuanerp.passport.service.IUserService;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.UserAddNewDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.UserLoginDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.UserUpdateDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.UserDetailsVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.UserListItemVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户控制层
 *
 * @author gudiwen
 * @version 0.0.1
 */
@Api(tags = "1.用户模块")
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    @Autowired
    IUserService service;

    @GetMapping("/test")
    public String test(){
        return "ok";
    }

    @ApiOperation("用户密码登录")
    @ApiOperationSupport(order = 5)
    @PostMapping("/login")
    public JsonResult<String> login(@Valid UserLoginDTO userLoginDTO, HttpServletRequest request){
        log.debug("开始处理[根据密码登录用户]的请求,请求参数userLoginDTO:{}",userLoginDTO);
        String jwt = service.login(userLoginDTO,request);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("用户登录(验证码)")
    @ApiOperationSupport(order = 10)
    @PostMapping("/loginByValidCode")
    public JsonResult<String> loginByValidCode(@Valid UserLoginDTO userLoginDTO,HttpServletRequest request){
        log.debug("开始处理[根据验证码登录用户]的请求,请求参数userLoginDTO:{}",userLoginDTO);
        String jwt = service.loginByValidCode(userLoginDTO,request);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("验证原密码")
    @ApiOperationSupport(order = 15)
    @PostMapping("/validPassword")
    public JsonResult<Void> validPassword(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal,String password){
        log.debug("开始处理[验证原密码]的请求");
        service.validPassword(loginPrincipal.getId(),password);
        return JsonResult.ok();
    }

    @ApiOperation("用户退出")
    @ApiOperationSupport(order = 20)
    @PostMapping("/logout")
    public JsonResult<Void> logout(){
        log.debug("开始处理[用户退出]的请求");
        service.logout();
        return JsonResult.ok();
    }


    @ApiOperation("新增用户")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('staff_manage')")
    @PostMapping("/add-new")
    public JsonResult<Void> insert(@Valid UserAddNewDTO addNewDTO){
        log.debug("开始处理[新增用户]的请求,请求参数addNewDTO:{}",addNewDTO);
        service.addNew(addNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('staff_manage')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理[删除用户]的请求,请求参数id:{}",id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("用户列表")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('staff_list')")
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list(){
        log.debug("开始处理[查询用户列表]的请求");
        return JsonResult.ok(service.list());
    }

    @ApiOperation("根据姓名模糊查询用户列表")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('staff_list')")
    @GetMapping("/listByUsername/{username}")
    public JsonResult<List<UserListItemVO>> listByUsername(@PathVariable String username){
        log.debug("开始处理[根据姓名模糊查询用户列表]的请求");
        return JsonResult.ok(service.listByUsername(username));
    }

    @ApiOperation("根据登录名模糊查询用户列表")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('staff_list')")
    @GetMapping("/listByLoginName/{loginName}")
    public JsonResult<List<UserListItemVO>> listByLoginName(@PathVariable String loginName){
        log.debug("开始处理[根据姓名模糊查询用户列表]的请求");
        return JsonResult.ok(service.listByLoginName(loginName));
    }

    @ApiOperation("根据是否启用查询用户列表")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('staff_list')")
    @GetMapping("/listByEnable/{enable}")
    public JsonResult<List<UserListItemVO>> listByEnable(@PathVariable Integer enable){
        log.debug("开始处理[根据是否启用查询用户列表]的请求");
        return JsonResult.ok(service.listByEnable(enable));
    }

    @ApiOperation("用户详情")
    @ApiOperationSupport(order = 310)
    @GetMapping("/userDetails")
    public JsonResult<UserDetailsVO> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始处理[查询用户详情]的请求");
        return JsonResult.ok(service.getUserDetailsVOById(loginPrincipal.getId()));
    }

    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 400)
    @PostMapping("/updateById/{id}")
    public JsonResult<Void> updateById(@PathVariable Long id, UserUpdateDTO userUpdateDTO){
        log.debug("开始处理[修改用户详情]的请求");
        service.updateById(id,userUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long")
    @PreAuthorize("hasAuthority('staff_manage')")
    @GetMapping("/update/{id}/enable")
    public JsonResult updateEnable(@PathVariable Long id){
        log.debug("开始处理修改启用状态业务");
        service.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "long")
    @PreAuthorize("hasAuthority('staff_manage')")
    @GetMapping("/update/{id}/disable")
    public JsonResult updateDisEnable(@PathVariable Long id){
        log.debug("开始处理修改禁用状态业务");
        service.setDisEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("重置密码")
    @ApiOperationSupport(order = 402)
    @PreAuthorize("hasAuthority('staff_manage')")
    @PostMapping("/{authentication}/forgetPassword")
    public JsonResult<Void> forgetPassword(@PathVariable String authentication){
        log.debug("开始处理[重置密码]的请求,请求参数authentication:{}",authentication);
        service.forgetPassword(authentication);
        return JsonResult.ok();
    }

    @ApiOperation("发送验证码")
    @ApiOperationSupport(order = 500)
    @GetMapping("/sendValidCode/{phone}")
    public JsonResult<Void> sendValidCode(@PathVariable String phone){
        log.debug("开始处理[发送验证码]的业务");
        service.sendValidCode(phone);
        return JsonResult.ok();
    }
}
